import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ThemeData(
          useMaterial3: false,
          primarySwatch: Colors.blue,
        ),
        home: const MyHome()
    );
  }
}

class MyHome extends StatefulWidget {
  const MyHome({super.key});

  @override
  State<MyHome> createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {

  final GlobalKey _globalKey1 = GlobalKey(debugLabel: "1");
  final GlobalKey _globalKey2 = GlobalKey(debugLabel: "2");
  final GlobalKey _globalKey3 = GlobalKey(debugLabel: "3");

  List<Widget> list = [];

  @override
  void initState() {
    super.initState();
    list = [
      Box(key: _globalKey1, color: Colors.red),
      Box(key: _globalKey2, color: Colors.blue),
      Box(key: _globalKey3, color: Colors.yellow),
    ];
  }

  @override
  Widget build(BuildContext context) {
    var orientation = MediaQuery.of(context).orientation;

    return Scaffold(
      appBar: AppBar(
        title: const Text("Flutter App"),
      ),
      body:  Center(
        child: orientation == Orientation.portrait ?  Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: list,
        ):Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: list,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            list.shuffle();
          });
        },
        child: const Icon(Icons.refresh),
      ),
    );
  }
}


class Box extends StatefulWidget {

  final Color color;

  const Box({super.key,required this.color});

  @override
  State<Box> createState() => _BoxState();
}

class _BoxState extends State<Box> {

  int _count = 0;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 100,
      height: 100,
      child: ElevatedButton(
        style: ButtonStyle(
          backgroundColor: MaterialStateProperty.all(widget.color),
        ),
        onPressed: () {
          setState(() {
            _count++;
          });
        },
        child: Text("$_count",style: const TextStyle(fontSize: 30)),
      ),
    );
  }
}

